<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['construction:team:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['construction:team:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['construction:team:remove']">删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="类别ID" align="center" prop="teamId" />
        <el-table-column label="类别名称" align="center" prop="teamName" :show-overflow-tooltip="true" />
        <el-table-column label="类别编号" align="center" prop="teamCode" :show-overflow-tooltip="true" />
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.teamStatus" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['construction:team:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['construction:team:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别名称" prop="teamName">
              <el-input v-model="form.teamName" placeholder="请输入类别名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.teamStatus">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

  import { listTeam,getTeam,addTeam,updateTeam,deleteTeam,changeTeamStatus } from "@/api/construction/team";


  export default {
    name: "Team",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        teamList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label",
        },
        // 表单校验
        rules: {
          teamName: [
            { required: true, message: "类别名称不能为空", trigger: "blur" },
          ],
        },
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then((response) => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.teamId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      // 类别状态修改
      handleStatusChange(row) {
        let text = row.teamStatus === "0" ? "启用" : "停用";
        this.$confirm(
          '确认要"' + text + '""' + row.teamName + '"用户吗?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return changeTeamStatus(row.teamId, row.teamStatus);
          })
          .then(() => {
            this.msgSuccess(text + "成功");
          })
          .catch(function () {
            row.teamStatus = row.teamStatus === "0" ? "1" : "0";
          });
      },

      /* 查询类别列表 **/
      getList(){
        this.loading = true;
        listTeam(this.queryParams).then(response => {
          this.teamList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

      // 表单重置
      reset() {
        this.form = {
          teamId: undefined,
          teamName: undefined,
          teamCode: undefined,
          teamStatus: "0",
        };
        this.resetForm("form");
      },

      /** 添加 */
      handleAdd(){
        this.reset();
        getTeam().then((response) => {
          this.open = true;
          this.title = "添加类别";
        });
      },

      /** 修改 */
      handleUpdate(row) {
        this.reset();
        const teamId = row.teamId || this.ids;
        getTeam(teamId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改类别";
        });
      },

      /** 删除 */
      handleDelete(row){
        const teamIds = row.teamId || this.ids;
        this.$confirm(
          '是否确认删除用户编号为"' + teamIds + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return deleteTeam(teamIds);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      },

      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.teamId != undefined) {
              updateTeam(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addTeam(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
    }
  }
</script>

<style scoped>

</style>
